<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\Admin;
use app\admin\model\Project;
use think\Session;
use think\Db;
/**
 * 会议纪要管理
 *
 * @icon fa fa-circle-o
 */
class Meetingcontent extends Backend
{
    
    /**
     * Meeting_content模型对象
     * @var \app\admin\model\Meetingcontent
     */
    protected $model = null;
    protected $noNeedRight = ['detail','admin','project'];
    protected $admin_id = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Meetingcontent;
        $this->admin_id = Session::get("admin")['id'];
    }

    
    public function index($ids=null,$status=null)
    {
        $this->view->assign("meeting_id",$ids);
        $this->view->assign("status",$status);
        if ($this->request->isAjax())
        {
            $map['status']=$_GET['status'];
            $map['meeting_id']=$_GET['ids'];
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if($this->admin_id == 1){
                $where1 = [];
            }else{
                $where1[] = ['exp',Db::raw("FIND_IN_SET($this->admin_id,user_id)")];
            }
            $where2['del']=0;
            $total = $this->model
                ->with('admin','project')
                ->where($where)
                ->where($map)
                ->where($where1)
                ->where($where2)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                 ->with('admin','project')
                ->where($where)
                ->where($map)
                ->where($where1)
                ->where($where2)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
           
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
      
        return $this->view->fetch();
    }



    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $param = $this->request->param();
        $this->view->assign("meeting_id", $param['meeting_id']);
        $this->view->assign("status", $param['status']);
        return $this->view->fetch();
    }

       //用户
       public function admin(){
        $list = Admin::field('nickname,id')->select();
        if ($list){
            $arr = [];
            foreach($list as $k => $v){
                $arr[$v['id']] = $v['nickname'];
            }
            $json = json($arr);
        }
        return $json;
    }


       //项目
       public function project(){
        $list = Project::field('title,id')->select();
        if ($list){
            $arr = [];
            foreach($list as $k => $v){
                $arr[$v['id']] = $v['title'];
            }
            $json = json($arr);
        }
        return $json;
    }


    
    public function detail($ids){
        $xjlist = db("meeting_content")->where("id",$ids)->find();
        $adminlist= db("admin")->where('id','in',$xjlist['user_id'])->select();
        $projectlist= db("project")->where('id','in',$xjlist['project_id'])->select();
        $admin=[];
        foreach($adminlist as $key=>$val){
            $admin[$key]=$val['nickname'];
        }
        $project=[];
        foreach($projectlist as $key1=>$val1){
            $project[$key1]=$val1['title'];
        }
     

        $xjlist['user_id']=implode(',',$admin);
        $xjlist['project_id']=implode(',',$project);
        $this->assign('xjlist',$xjlist);
        return $this->fetch();
    }

    public function import()
    {
        parent::import();
    }

    public function del($ids = "")
    {
        if ($ids)
        {
            $map['deletetime']=time();
            $map['del']=1;    
            $count = $this->model->where('id', 'in', $ids)->update($map);
            if ($count)
            {
                $this->success();
            }
        }
        $this->error();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

}
